#include <bits/stdc++.h>
using namespace std;

const int N = 5e6+50;

vector<pair<char, int>> trie[N];

int mi[N], dp[N];

int curRank;

int ans[N];
int n, m, a[N];

bool in[N];

void dfs(int u, int f) {
    if (~f)
        dp[u] = dp[f] + 1;

    int tmp = curRank;

    if (in[u]) {
        ++curRank;
        dp[u] = min(dp[u], mi[f] + curRank); 
    }

    mi[u] = dp[u] - tmp;
    if (~f)
        mi[u] = min(mi[f], mi[u]);

    for (const auto &p : trie[u]) {
        dfs(p.second, u);
    }
}


int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    int root = 0;
    for (int i=1; i<=n; ++i) {
        int fa;
        char s[10];
        scanf("%d%s", &fa, s);
        trie[fa].push_back(make_pair(s[0], i));
    }

    for (int i=0; i<=n; ++i)
        sort(trie[i].begin(), trie[i].end());

    memset(dp, 0x3f3f3f3f, sizeof dp);
    memset(mi, 0x3f3f3f3f, sizeof mi);

    dp[0] = mi[0] = 0;

    scanf("%d", &m);
    for (int i=1; i<=m; ++i) {
        scanf("%d", &a[i]);
        in[a[i]] = 1;
    }

    dfs(0, -1);

    for (int i=1; i<=m; ++i)
        printf("%d%c", dp[a[i]], i==m? '\n':' ');

    return 0;
}